package com.apply.controller;

import com.apply.config.Results;
import com.apply.entity.ExamInfo;
import com.apply.service.ExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

/**
 * @author tianp
 */
@RestController
@RequestMapping("/private/exam/")
@Api(value = "exam", tags = "报名考试")
public class ExamController {
    @Autowired
    private ExamService examService;

    @ApiOperation(value = "新增考试信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examName", value = "考试名称", dataType = "string"),
            @ApiImplicitParam(name = "applyStart", value = "报名开始时间", dataType = "date"),
            @ApiImplicitParam(name = "applyEnd", value = "报名结束时间", dataType = "date"),
            @ApiImplicitParam(name = "examStart", value = "考试开始时间", dataType = "date"),
            @ApiImplicitParam(name = "examEnd", value = "考试结束时间", dataType = "date"),
            @ApiImplicitParam(name = "examLocation", value = "考试地点", dataType = "string"),
            @ApiImplicitParam(name = "payMoney", value = "报名费用", dataType = "double"),
    })
    @PostMapping("save")
    public Results saveExam(/*@RequestBody JSONObject object*/ ExamInfo examInfo) throws Exception {
        examService.save(examInfo);
        return Results.success();
    }

    @ApiOperation(value = "根据id查询考试信息")
    @ApiImplicitParam(name = "id", value = "考试信息id", dataType = "long")
    @GetMapping("findOne")
    public Results findOne(@RequestParam(name = "id", required = true) Long id) {
        return Results.success(examService.findOne(id));
    }

    @ApiOperation(value = "分页查询所有的考试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", required = true),
            @ApiImplicitParam(name = "examName", value = "考试名称", dataType = "string")
    })
    @GetMapping("findAll")
    public Results findAll(String examName, Integer pageNum) {
        return Results.success(examService.findAllByPage(examName, pageNum));
    }

    @ApiOperation(value = "可选择的修改考试信息")
    @PostMapping("updateBySelective")
    public Results updateBySelective(ExamInfo examInfo) throws Exception {
        if (null == examInfo.getId()) {
            return Results.error(400, "考试id不能为空！");
        }
        convertParam(examInfo);
        return Results.success(examService.updateBySelective(examInfo));
    }

    public void convertParam(ExamInfo examInfo) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != examInfo.getApplyStartStr()) {
            examInfo.setApplyStart(sdf.parse(examInfo.getApplyStartStr()));
        }
        if (null != examInfo.getApplyEndStr()) {
            examInfo.setApplyEnd(sdf.parse(examInfo.getApplyEndStr()));

        }
        if (null != examInfo.getExamStartStr()) {
            examInfo.setExamStart(sdf.parse(examInfo.getExamStartStr()));

        }
        if (null != examInfo.getExamEndStr()) {
            examInfo.setExamEnd(sdf.parse(examInfo.getExamEndStr()));

        }
    }
}

